<?php
/*
*	$version: 2009-6-24 17:46:24 beta 1.0
*	$id: Page.class.php 
*	$author: taoFeng
*	$mailto: font99@Gmail.com
*	$license: This is not a freeware
*/

class Page {

	private $startNum;
	private $limitNum;
	private $countNum;
	private $scriptName;
	private $allPages;
	private $maxPages;
	private $isUseDoor = 1;

	public function setCountNum($_countNum) {
		if (is_array($_countNum)) {
			$this->countNum = intval(array_shift($_countNum));
		}
		else {
			$this->countNum = intval($_countNum);
		}
	}

	public function setIsUseDoor($_isUseDoor) {
		$this->isUseDoor = $_isUseDoor;
	}

	public function getStartNum() {
		return $this->startNum;
	}

	public function getLimitNum() {
		return $this->limitNum;
	}

	function __construct($_scriptName,$_countNum,$_startNum = 0,$_limitNum = 10,$_maxPages = 0) {
		global $_G;
		$this->scriptName = empty($_scriptName) ? $_G['phpSelf'] : $_scriptName;
		$this->startNum = intval($_startNum);
		$this->limitNum = intval($_limitNum);
		$this->setCountNum($_countNum);
		$this->allPages = 1;
		$this->maxPages = intval($_maxPages);
	}

	public function show() {
		
		if ($GLOBALS['__page'] <= 1 || empty($GLOBALS['__page'])) {
			$startNum = 0;
		}
		else {
			$startNum = ($GLOBALS['__page'] - 1) * $this->limitNum;
		}

		$this->startNum = $startNum;

		return $this->mulit();
	}

	public function mulit() {

		$pageString = '';

		if (strpos($this->scriptName, '?') !== false)
			$this->scriptName .= "&";
		else
			$this->scriptName .= "?";

		$currPage = 1;
		$prevPage = 1;

		if ($GLOBALS['__page'] > 1) {
			$prevPage = $GLOBALS['__page'] - 1;
			$currPage = $GLOBALS['__page'];
		}

		if ($currPage < 1 || $GLOBALS['__page'] < 1) {
			$prevPage = 1;
			$currPage = 1;
		}

		$this->allPages = @ceil ($this->countNum / $this->limitNum);

		if ($this->maxPages > 0) {
			if ($this->allPages > $this->maxPages)
				$this->allPages = $this->maxPages;
			
			if ($currPage > $this->maxPages)
				$currPage = $this->maxPages;
		}

		if ($this->allPages < 1) {
			$this->allPages = 1;
		}

		if ($currPage > $this->allPages) {
			$currPage = $this->allPages;
		}

		if ($this->allPages <= $GLOBALS['__page']) {
			$nextPage = $this->allPages;
		}
		else {
			$nextPage = $GLOBALS['__page'] + 1;
		}

		$pageString = '';

		if ($currPage > 1) {
			$pageString = '<a href="[script]page=[prevpage]">[prevname]</a>';
		}

		for ($i = $currPage - 5; $i <= $currPage + 5; $i++) {
			if ($i >= 1 && $i <= $this->allPages) {
				if ($currPage == $i)
					$pageString .= '&nbsp;[<a href="#" class="selected">' . $i . '</a>]';
				else
					$pageString .= '&nbsp;<a href="[script]page=' . $i . '">' . $i . '</a>';
			}
		}

		if ($currPage < $this->allPages) {
			$pageString .= '&nbsp;<a href="[script]page=[nextpage]">[nextname]</a>';
		}

		if ($this->isUseDoor == 1) {
			if ($this->allPages > 0) {
				$pageString = '<a href="[script]page=1">[indexname]</a>&nbsp;' . $pageString . '&nbsp;<a href="[script]page=[lastpage]">[lastname]</a>';
			}
		}

		return $this->setTpl($pageString,$prevPage,$nextPage);
	}

	public function setTpl($_pageString,$_prevPage,$_nextPage) {

		$tagArray = array (
			'[script]' , '[prevpage]' , '[prevname]' , '[nextpage]' , '[nextname]' , '[indexname]' , '[lastpage]' , '[lastname]'
		);

		$replaceArray = array (
			$this->scriptName, $_prevPage, '前页', $_nextPage, '后页', '首页', $this->allPages, '末页' 
		);

		return str_replace($tagArray,$replaceArray,$_pageString);
	}

}

?>